<?php
require_once 'config.php';
require_once 'auth.php';
requireLogin();

// 每页显示的记录数
$limit = 10;

// 当前页码
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// 搜索条件
$searchUser = isset($_GET['user']) ? $_GET['user'] : '';
$searchDate = isset($_GET['date']) ? $_GET['date'] : '';

// 构建 SQL 查询
$sql = "SELECT sr.id, i.product_name, sr.quantity, sr.operation_type, u.username, sr.operate_at 
        FROM stock_records sr
        JOIN inventory i ON sr.item_id = i.id
        JOIN users u ON sr.operator_id = u.id
        WHERE 1=1";

if (!empty($searchUser)) {
    $sql .= " AND u.username LIKE '%$searchUser%'";
}

if (!empty($searchDate)) {
    $sql .= " AND DATE(sr.operate_at) = '$searchDate'";
}

// 计算总记录数
$countSql = str_replace("SELECT sr.id, i.product_name, sr.quantity, sr.operation_type, u.username, sr.operate_at", "SELECT COUNT(*) as total", $sql);
$conn = dbConnect();
$resultCount = $conn->query($countSql);
$rowCount = $resultCount->fetch_assoc();
$total = $rowCount['total'];

// 计算总页数
$totalPages = ceil($total / $limit);

// 添加分页限制
$sql .= " LIMIT $start, $limit";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>出入库记录</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>

<body class="layout-fluid layout-vertical">
    <?php include 'navbar.php'; ?>
    <div class="page">
        <div class="container-fluid">
            <div class="card mt-3">
                <div class="card-header">
                    <h3 class="card-title">出入库记录</h3>
                </div>
                <div class="card-body">
                    <form method="get" class="mb-3">
                        <div class="row">
                            <div class="col-md-4">
                                <input type="text" name="user" class="form-control" placeholder="按用户搜索" value="<?php echo $searchUser; ?>">
                            </div>
                            <div class="col-md-4">
                                <input type="date" name="date" class="form-control" placeholder="按日期搜索" value="<?php echo $searchDate; ?>">
                            </div>
                            <div class="col-md-4">
                                <button type="submit" class="btn btn-primary">搜索</button>
                            </div>
                        </div>
                    </form>
                    <div class="table-responsive"> <!-- 添加table-responsive -->
                        <table class="table table-vcenter table-hover"> <!-- 修改表格类 -->
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>物品名称</th>
                                    <th>数量</th>
                                    <th>操作类型</th>
                                    <th>操作人员</th>
                                    <th>操作时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = $result->fetch_assoc()): ?>
                                    <tr>
                                        <td><?= $row['id'] ?></td> <!-- 统一使用短标签 -->
                                        <td><?= htmlspecialchars($row['product_name']) ?></td>
                                        <td><?= $row['quantity'] ?></td>
                                        <td><?= $row['operation_type'] ?></td>
                                        <td><?= htmlspecialchars($row['username']) ?></td>
                                        <td><?= $row['operate_at'] ?></td>
                                    </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                    <?php include 'pagination.php'; ?>
                </div>
            </div>
        </div>
    </div>
    <script src="/dist/js/tabler.min.js"></script>
</body>

</html>